package com.zeng.sim.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONUtil;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;

import javax.xml.xpath.XPathConstants;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-22T16:58
 */
public class MsgUtil {

    public static boolean isJson(String msgContent) {
        return JSONUtil.isJson(msgContent);
    }

    public static boolean isXml(String msgContent) {
        try {
            XmlUtil.parseXml(msgContent);
        }
        catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 获取渠道号
     * @param json
     * @return
     */
    public static String getChannelNoByJsonPath(String json) {
        String jsonPathStr = SysConfigUtil
            .getSystemConfigValue(SysConfigUtil.CONFIG_CODE_SERVER_SIM_CHANNEL_NO_JSONPATH);
        if (!StringUtils.hasLength(jsonPathStr)) {
            return SimConstant.MSG_CHANNEL_NO_NONE;
        }
        String[] jsonPathArr = jsonPathStr.split(StrUtil.COMMA);
        DocumentContext documentContext = JsonPath.parse(json);
        String channelNo = null;
        for (String jsonPath : jsonPathArr) {
            channelNo = documentContext.read(jsonPath, String.class);
            if (StringUtils.hasLength(channelNo)) {
                return channelNo;
            }
        }
        return SimConstant.MSG_CHANNEL_NO_NONE;
    }

    /**
     * 获取渠道号
     * @param xml
     * @return
     */
    public static String getChannelNoByXPath(String xml) {
        String xPathStr = SysConfigUtil.getSystemConfigValue(SysConfigUtil.CONFIG_CODE_SERVER_SIM_CHANNEL_NO_XPATH);
        if (!StringUtils.hasLength(xPathStr)) {
            return SimConstant.MSG_CHANNEL_NO_NONE;
        }
        Document document = XmlUtil.parseXml(xml);
        String[] xPathArr = xPathStr.split(StrUtil.COMMA);
        String channelNo = null;
        for (String xPath : xPathArr) {
            channelNo = (String) XmlUtil.getByXPath(xPath, document, XPathConstants.STRING);
            if (StringUtils.hasLength(channelNo)) {
                return channelNo;
            }
        }
        return SimConstant.MSG_CHANNEL_NO_NONE;
    }

    /**
     * 获取报文编码
     * @param json
     * @return
     */
    public static String getMsgCodeByJsonPath(String json) {
        String jsonPathStr = SysConfigUtil.getSystemConfigValue(SysConfigUtil.CONFIG_CODE_SERVER_SIM_MSG_CODE_JSONPATH);
        String[] jsonPathArr = jsonPathStr.split(StrUtil.COMMA);
        DocumentContext documentContext = JsonPath.parse(json);
        String msgCode = null;
        for (String jsonPath : jsonPathArr) {
            try {
                msgCode = documentContext.read(jsonPath, String.class);
                if (StringUtils.hasLength(msgCode)) {
                    return msgCode;
                }
            }
            catch (PathNotFoundException e) {
                // ignore
            }
        }
        throw new IllegalArgumentException("获取报文编码为空，请检查配置[模拟服务端-json报文-报文编码JsonPath]");
    }

    /**
     * 获取报文编码
     * @param xml
     * @return
     */
    public static String getMsgCodeByXPath(String xml) {
        Document document = XmlUtil.parseXml(xml);
        String xPathStr = SysConfigUtil.getSystemConfigValue(SysConfigUtil.CONFIG_CODE_SERVER_SIM_MSG_CODE_XPATH);
        String[] xPathArr = xPathStr.split(StrUtil.COMMA);
        String msgCode = null;
        for (String xPath : xPathArr) {
            msgCode = (String) XmlUtil.getByXPath(xPath, document, XPathConstants.STRING);
            if (StringUtils.hasLength(msgCode)) {
                return msgCode;
            }
        }
        throw new IllegalArgumentException("获取报文编码为空，请检查配置[模拟服务端-xml报文-报文编码XPath]");
    }

}
